var api = {
    initGoods:function(req){
        req['serverName']  = '/wx/goods/detail';
        doRequestwithnoheader(req).then(function(res){
            $('.js-goods-template').append(template('js-goods-template',{data:res.data,apiDomain:getApiDomain()}));
        });
    },
    commitOrder:function(){
        var consignee = $('#consignee').val();
        var address = $('#address').val();
        var phone = $('#phone').val();
        var recommend_code = $('#recommend_code').val();

        if(!consignee){
            alert('请填写收货人');
            return;
        }

        if(!address){
            alert('请填写地址');
            return;
        }

        if(!phone){
            alert('请填写手机号');
            return;
        }

        var req = {};
        req['serverName']  = '/wx/indent/create'
        req['consignee']  = consignee;
        req['address']  = address;
        req['phone']  = phone;
        req['recommend_code']  = recommend_code;
        req['goods_id'] = g_params['goods_id'];

        doRequestwithnoheader(req).then(function(res){
            window.location.href = '/wx/html/order_detail.html?indent_id='+res.data.indent_id;
        });
    }
}

var init = (function(){
    // 加载公共头部
    main_api.loadCommonHeader({'title':'生成订单','left':'back','right':''})

    /*加载数据*/
    //获取商品ID
    var goods_id = g_params['goods_id'];
    api.initGoods({goods_id:goods_id});

    /*监听下单*/
    $('#commitOrder').click(function(){
        api.commitOrder();
    });
})(init)
